package com.huawei.crm.service.impl;

import com.huawei.crm.service.TemplateExcelService;
import com.huawei.crm.util.ExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

/**
 * Excel模板服务接口实现
 *
 * @author caohy
 * @version 2021年7月12日下午1:56:54
 */
@Slf4j
@Service
public class TemplateExcelServiceImpl implements TemplateExcelService {


    /**
     * 资源文件加载器
     */
    private ResourceLoader resourceLoader = new DefaultResourceLoader();

    @Override
    public boolean exportExcel(String templateName, Map<String, Object> model, OutputStream out) {
        try {
            // 获取模板文件的输入流
            InputStream inputStream = new FileInputStream("templatePath" + templateName);
            ExcelUtils.exportExcel(inputStream, model, out);

        } catch (IOException e) {
            log.error("excel export has error", e);
            return false;
        }
        return true;
    }

    @Override
    public boolean exportExcel(String templateName, Map<String, Object> model, String fileName,
                               HttpServletResponse response) {
        try {
            // 获取模板文件的输入流
            InputStream inputStream = new FileInputStream("templatePath" + templateName);
            ExcelUtils.exportExcel(inputStream, model, fileName, response);

        } catch (IOException e) {
            log.error("excel export has error", e);
            return false;
        }
        return true;
    }

}
